package blue.logger;

import org.zeromq.ZMQ;

public class Publisher{
	private ZMQ.Context zmq_ctx_;
	private ZMQ.Socket zmq_sock_;	
	private String port_;

	public Publisher(String port){
        //  Prepare our context and publisher
        port_ = port;
        String endpoint = "tcp://*:"+port_;
        zmq_ctx_ = ZMQ.context(1);
        zmq_sock_ = zmq_ctx_.socket(ZMQ.PUB);
        zmq_sock_.bind(endpoint);
        System.out.println("Publish Message to "+endpoint);
	}

	public void publish(String msg){
        zmq_sock_.send(msg, 0);
	}

}


// package main;

// import java.util.Random;
// import org.zeromq.ZMQ;

// //
// //  Weather update server in Java
// //  Binds PUB socket to tcp://*:5556
// //  Publishes random weather updates
// //
// public class Wuserver {

//     public static void main (String[] args) throws Exception {
//         //  Prepare our context and publisher
//         ZMQ.Context context = ZMQ.context(1);

//         ZMQ.Socket publisher = context.socket(ZMQ.PUB);
//         publisher.bind("tcp://*:5556");
//         publisher.bind("ipc://weather");

//         //  Initialize random number generator
//         Random srandom = new Random(System.currentTimeMillis());
//         while (!Thread.currentThread ().isInterrupted ()) {
//             //  Get values that will fool the boss
//             int zipcode, temperature, relhumidity;
//             zipcode = 10000 + srandom.nextInt(10000) ;
//             temperature = srandom.nextInt(215) - 80 + 1;
//             relhumidity = srandom.nextInt(50) + 10 + 1;

//             //  Send message to all subscribers
//             String update = String.format("%05d %d %d", zipcode, temperature, relhumidity);
//             publisher.send(update, 0);
//         }

//         publisher.close ();
//         context.term ();
//     }
// }
